<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
		<el-aside width="15%" v-loading="menuloading">
			<el-container>
                <el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''"
							 draggable highlight-current :expand-on-click-node="false"
							 :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
            </el-container>
		</el-aside>
		<el-container>
			<el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back">确定</el-button>
                </div>
                <div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="请输入关键词" clearable @change="handleKeyDown"></el-input>
                        <el-select style="margin:5px 10px" v-model="search.unit_id" filterable clearable placeholder="请选择单位">
                            <el-option v-for="item in unitList" :key="item.id" :label="item.unit_name" :value="item.id"/>
                        </el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                        <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
					</div>
				</div>
            </el-header>
			<el-main class="nopadding">
				<scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="600">
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                    <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
					<el-table-column label="编号" prop="work_code" align="center"></el-table-column>
					<el-table-column label="名称" prop="work_name" align="center"></el-table-column>
					<el-table-column label="委外" prop="is_out" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.is_out == 1 ? 'success' : 'danger'">{{scope.row.is_out == 1 ?'是':'否'}}</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="工资计价方式" prop="valuation_type" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.valuation_type == 1 ? 'success' : 'primary'">{{scope.row.valuation_type == 1 ?'计件':'计时'}}</el-tag>
                        </template>
                    </el-table-column>
					<el-table-column label="报工权限" prop="work_power" align="center">
                        <template #default="scope">
                            {{scope.row.work_power == 1 ? '班组' : '人员'}}
                        </template>
                    </el-table-column>
					<el-table-column label="不良品项" prop="rejects_all" align="center"></el-table-column>
					<el-table-column label="创建人" prop="create_staff" align="center"></el-table-column>
					<el-table-column label="创建时间" prop="create_time" align="center">
                        <template #default="scope">
                            {{moment(scope.row.create_time * 1000).format('YYYY-MM-DD')}}1
                        </template>
                    </el-table-column>
				</scTable>
			</el-main>
		</el-container>
	    </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择物料',
				route: '选择物料',
				edit: '编辑物料',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			apiObj: this.$API.craft.craft.list,
			selection: [],
			search: {
				work_name: null,
			},
			menuList: [],
			menuProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			menuloading: false,
			menuFilterText: "",
			menuForm: {},
		}
	},
    watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
	mounted() {
        this.getMenu();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.index = data
		},
        //加载树数据
		async getMenu() {
			this.menuloading = true;
			var res = await this.$API.craft.craftCate.list.get();
			this.menuloading = false;
			var allNode = {id: '', cate_name: '所有'}
			res.data.unshift(allNode);
			this.menuList = res.data;
			// this.menuClick(allNode)
		},
		//树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				cate_id: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
        // 选择事件
        selectionChange(selection) {
			this.selection = selection;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.mode,this.index)
            this.visible = false;
        },
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        // 重置
        clearSearch(){
            this.search.text_select = null
        },
	}
}
</script>

<style></style>
